/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class frmThi extends javax.swing.JFrame {

    /**
     * Creates new form frmThi
     */
    public frmThi() {
        initComponents();
    }
    private List<BaiThi> dsBaiThi = new ArrayList<BaiThi>();
    private DeThi DETHI;
    private int baiThiHienTai;
    private int dapAnDaChon = -1;
    private int thoiGian = 10; //Thoi gian 10 phut de thi
    Thread th;
    private boolean exit = false;

    public DeThi getDETHI() {
        return DETHI;
    }

    public void setDETHI(DeThi DETHI) {
        this.DETHI = DETHI;
        loadDSBaiThi();
        demNguocThoiGian();
    }

    private void demNguocThoiGian() {
        final frmThi frm = this;
        th = new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i = thoiGian; i >= 0; i--) {
                    for (int j = 59; j >= 0; j--) {
                        if (exit) {
                            return;
                        }
                        try {
                            Thread.sleep(1000);
                            lblThoiGian.setText(i + ":" + j);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }

                }
                java.util.Date d = new java.util.Date();
                Date ngayThi = new Date(d.getTime());
                DiemThi dt = new DiemThi();
                dt.setDiem(tinhDiem());
                dt.setMaDeThi(DETHI.getMaDe());
                dt.setNgayThi(ngayThi);
                dt.setUserName(Main.nguoiDungDangNhap.getUserName());
                dt.themDiemThi();

                JOptionPane.showMessageDialog(null, "Hết thời gian thi\n Bạn đạt "+tinhDiem()+" điểm", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                frm.dispose();
            }
        });
        th.start();
    }

    private int tinhDiem() {
        int diem = 0;
        for (int i = 0; i < dsBaiThi.size(); i++) {
            BaiThi bt = dsBaiThi.get(i);
            if (bt.getDapAnDung() == bt.getDapAnDaChon()) {
                diem++;
            }
        }
        return diem;
    }

    private void loadDSBaiThi() {
        dsBaiThi = BaiThi.layBaiThiTheoMaDe(this.DETHI.getMaDe());
        if (dsBaiThi.size() > 0) {
            BaiThi bt = dsBaiThi.get(0);
            hienThiCauHoi(bt);
        }
        baiThiHienTai = 0;
    }

    void hienThiCauHoi(BaiThi bt) {
        lblDeBai.setText(bt.getDeBai());
        radDapAn1.setText(bt.getDapAn1());
        radDapAn2.setText(bt.getDapAn2());
        radDapAn3.setText(bt.getDapAn3());
        radDapAn4.setText(bt.getDapAn4());
        switch (bt.getDapAnDaChon()) {
            case 1:
                radDapAn1.setSelected(true);
                break;
            case 2:
                radDapAn2.setSelected(true);
                break;
            case 3:
                radDapAn3.setSelected(true);
                break;
            case 4:
                radDapAn4.setSelected(true);
                break;
            default:
                buttonGroup1.clearSelection();
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        lblDeBai = new javax.swing.JLabel();
        btnLui = new javax.swing.JButton();
        btnToi = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        lblThoiGian = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        radDapAn4 = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        radDapAn3 = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        radDapAn2 = new javax.swing.JRadioButton();
        radDapAn1 = new javax.swing.JRadioButton();
        btnHoanTat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Đề bài:");

        lblDeBai.setText(".........");

        btnLui.setText("<< Lùi");
        btnLui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuiActionPerformed(evt);
            }
        });

        btnToi.setText("Tới >>");
        btnToi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToiActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Thời gian còn lại:");

        lblThoiGian.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        lblThoiGian.setForeground(new java.awt.Color(255, 0, 51));
        lblThoiGian.setText("00:00");

        jLabel2.setText("1.");

        buttonGroup1.add(radDapAn4);
        radDapAn4.setText("Đáp án 4");
        radDapAn4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radDapAn4ActionPerformed(evt);
            }
        });

        jLabel4.setText("4.");

        jLabel3.setText("2.");

        buttonGroup1.add(radDapAn3);
        radDapAn3.setText("Đáp án 3");
        radDapAn3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radDapAn3ActionPerformed(evt);
            }
        });

        jLabel5.setText("3.");

        buttonGroup1.add(radDapAn2);
        radDapAn2.setText("Đáp án 2");
        radDapAn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radDapAn2ActionPerformed(evt);
            }
        });

        buttonGroup1.add(radDapAn1);
        radDapAn1.setText("Đáp án 1");
        radDapAn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radDapAn1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(radDapAn1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(radDapAn4, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                    .addComponent(radDapAn3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(radDapAn2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(radDapAn1))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(radDapAn2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(radDapAn3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(radDapAn4))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        btnHoanTat.setText("Hoàn tất");
        btnHoanTat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHoanTatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(162, 162, 162)
                .addComponent(btnLui)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnHoanTat)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnToi)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnThoat))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblThoiGian, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblDeBai, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 12, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblDeBai, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLui)
                    .addComponent(btnToi)
                    .addComponent(btnHoanTat))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(lblThoiGian))
                .addGap(32, 32, 32)
                .addComponent(btnThoat)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        frmChonDeThi frm=new frmChonDeThi();
        frm.setVisible(true);
        this.dispose();

    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnToiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToiActionPerformed
        // TODO add your handling code here:
        if (baiThiHienTai + 1 <= dsBaiThi.size() - 1) {
            baiThiHienTai++;
            BaiThi bt = dsBaiThi.get(baiThiHienTai);
            hienThiCauHoi(bt);
        }
    }//GEN-LAST:event_btnToiActionPerformed

    private void btnLuiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuiActionPerformed
        // TODO add your handling code here:
        if (baiThiHienTai - 1 >= 0) {
            baiThiHienTai--;
            BaiThi bt = dsBaiThi.get(baiThiHienTai);
            hienThiCauHoi(bt);
        }
    }//GEN-LAST:event_btnLuiActionPerformed

    private void radDapAn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radDapAn1ActionPerformed
        // TODO add your handling code here:
        dapAnDaChon = 1;
        dsBaiThi.get(baiThiHienTai).setDapAnDaChon(dapAnDaChon);
    }//GEN-LAST:event_radDapAn1ActionPerformed

    private void radDapAn2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radDapAn2ActionPerformed
        // TODO add your handling code here:
        dapAnDaChon = 2;
        dsBaiThi.get(baiThiHienTai).setDapAnDaChon(dapAnDaChon);
    }//GEN-LAST:event_radDapAn2ActionPerformed

    private void radDapAn3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radDapAn3ActionPerformed
        // TODO add your handling code here:
        dapAnDaChon = 3;
        dsBaiThi.get(baiThiHienTai).setDapAnDaChon(dapAnDaChon);
    }//GEN-LAST:event_radDapAn3ActionPerformed

    private void radDapAn4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radDapAn4ActionPerformed
        // TODO add your handling code here:
        dapAnDaChon = 4;
        dsBaiThi.get(baiThiHienTai).setDapAnDaChon(dapAnDaChon);
    }//GEN-LAST:event_radDapAn4ActionPerformed

    private void btnHoanTatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHoanTatActionPerformed
        // TODO add your handling code here:
        java.util.Date d = new java.util.Date();
        Date ngayThi = new Date(d.getTime());
        DiemThi dt = new DiemThi();
        dt.setDiem(tinhDiem());
        dt.setMaDeThi(DETHI.getMaDe());
        dt.setNgayThi(ngayThi);
        dt.setUserName(Main.nguoiDungDangNhap.getUserName());
        dt.themDiemThi();
        JOptionPane.showMessageDialog(null, "Hoàn tất bài thi \n Bạn đạt đượct "+tinhDiem()+" điểm", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        frmChonDeThi frm=new frmChonDeThi();
        frm.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnHoanTatActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHoanTat;
    private javax.swing.JButton btnLui;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnToi;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblDeBai;
    private javax.swing.JLabel lblThoiGian;
    private javax.swing.JRadioButton radDapAn1;
    private javax.swing.JRadioButton radDapAn2;
    private javax.swing.JRadioButton radDapAn3;
    private javax.swing.JRadioButton radDapAn4;
    // End of variables declaration//GEN-END:variables
}
